import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';

import { systemid } from '../atom/config';

interface Message {
	asc: string[];
	filter: {
		equip_no: string;
		product_up_status: string;
	};
}

interface IAisle {
	_id: string;
	volume: string;
	aisle_name: string;
	equip_no: string;
	aisle_no: string;
}

interface IMerchandiseShelves {
	_id: string;
	companyid: string;
	equip_no: string;
	location_no: string;
	commodityid: string;
	warehouseid: string;
	picture: string;
	commodity_name: string;
	location_name: string;
	equip_name: string;
	warehousename: string;
	companyname: string;
	unit: string;
}

interface IZ001Time {
	_id: string;
	itemvalue: string;
	commodityid: string;
	warehouseid: string;
	location_no: string;
	itemname: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:d:\msz\gitee\works\w000030\src\app-supply_det\s001,action_id:' + action_id);


	const { exec, prepare } = query(action_id, session_id, systemid);
	prepare('merchandise_shelves', ['_id', 'companyid', 'equip_no', 'location_no', 'commodityid', 'warehouseid', 'picture', 'commodity_name', 'location_name', 'equip_name', 'warehousename', 'companyname', 'unit'], message.filter, 200, 1, message.asc, []);
	// r代表设备编号l代表库位编号
	const [data] = await exec<IMerchandiseShelves>();

	if (data.length === 0) {
		return {
			data: []
		};
	} else {
		const location_nos: string[] = [];
		const equip_no = message.filter.equip_no;
		const param = {
			$or: data.map((item) => {
				location_nos.push(item.location_no);
				return {
					commodityid: item.commodityid,
					warehouseid: item.warehouseid,
					location_no: item.location_no,
					itemname: '可用库存'
				};
			})
		};
		// console.log('设备编号===============》', equip_no);
		// console.log('库位编号===============》', location_nos);
		// console.log('条件===============》', param);
		const { exec: e2, prepare: p2 } = query(action_id, session_id, systemid);
		p2('aisle', ['_id', 'volume', 'aisle_name', 'equip_no', 'aisle_no'], { equip_no, aisle_no: { $in: location_nos } }, 200, 1, [], []);
		// 库容
		const [volume] = await e2<IAisle>();

		// 合并
		const M3 = volume.reduce((p, c) => {
			const obj = JSON.stringify({
				equip_no: c.equip_no,
				location_no: c.aisle_no
			});
			p.set(obj, c.volume);
			return p;
		}, new Map<string, string>());
		const data3 = data.map((i) => {
			const obj = JSON.stringify({
				equip_no: i.equip_no,
				location_no: i.location_no
			});
			return { ...i, volume: M3.get(obj) || 0 };
		});


		const { exec: e3, prepare: p3 } = query(action_id, session_id, systemid);
		p3('z001_time', ['_id', 'itemvalue', 'commodityid', 'warehouseid', 'location_no', 'itemname'], param, 200, 1, [], []);
		const [itemvalue] = await e3<IZ001Time>();

		// 合并
		const M2 = itemvalue.reduce((p, c) => {
			const obj = JSON.stringify({
				commodityid: c.commodityid,
				warehouseid: c.warehouseid,
				location_no: c.location_no
			});
			p.set(obj, c.itemvalue);
			return p;
		}, new Map<string, string>());
		const data2 = data3.map((i) => {
			const obj = JSON.stringify({
				commodityid: i.commodityid,
				warehouseid: i.warehouseid,
				location_no: i.location_no
			});
			return { ...i, itemvalue: M2.get(obj) || 0, number_storage: 0 };
		});
		return {
			data: data2
		};
	}
}
